package com.example.zbh.dsliao.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.constant.Constant;
import com.example.zbh.dsliao.model.entity.SearchSinger;
import com.example.zbh.dsliao.model.entity.SearchSong;
import com.example.zbh.dsliao.presenter.SearchPresenter;
import com.example.zbh.dsliao.ui.activty.Music_Detail_Activity;
import com.example.zbh.dsliao.ui.activty.SearchSingerActivity;
import com.example.zbh.dsliao.ui.adapter.SearchAdapter;
import com.example.zbh.dsliao.ui.viewinterface.ShowDownLoaderResultListener;
import com.example.zbh.dsliao.utils.net.VolleySingleton;
import com.example.zbh.dsliao.view.CustomDialog;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yaolin on 15/11/27.
 */
public class SearchFragment extends MVPBaseFragment<ShowDownLoaderResultListener, SearchPresenter> implements View.OnClickListener, TextWatcher, SearchAdapter.setSongOnClickListerner, SearchAdapter.setSignerOnClickListerner, ShowDownLoaderResultListener {
    private RecyclerView rVSearch;
    private SearchAdapter adapter;
    private ImageView search_img;
    private EditText searchEt;

    private String searchContent = "y"; //查询内容
    private Context context;

    //自定义dialog
    private CustomDialog customDialog;


    VolleySingleton volleySingleton = VolleySingleton.getInstance();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_search, null);
        initView(v);
        return v;
    }

    private void initView(View v) {
        rVSearch = (RecyclerView) v.findViewById(R.id.rVSearch);

        search_img = (ImageView) v.findViewById(R.id.search_img);
        searchEt = (EditText) v.findViewById(R.id.searchEt);
    }



    @Override
    protected void initData() {
        //初始化dialog
        customDialog = new CustomDialog(context, "玩命加载中...");

        GridLayoutManager gm = new GridLayoutManager(context, 1);
        rVSearch.setLayoutManager(gm);

        adapter = new SearchAdapter();
        rVSearch.setAdapter(adapter);

        adapter.setSongListerner(this);
        adapter.setSignerListener(this);

        search_img.setOnClickListener(this);
        searchEt.addTextChangedListener(this);
        createPresenter().startSearchConnection(getUrl());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_img:
                createPresenter().startSearchConnection(getUrl());
                break;
        }
    }


    public String getUrl(){
        String url = Constant.SEARCHFRONT
                + searchContent + Constant.SEARCHLATER;
        return url;
    }



    /**
     * EditText监听事件
     * **/
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        searchContent = s.toString();
        createPresenter().startSearchConnection(getUrl());

    }


    @Override
    public void onSongItemCick(List<SearchSong> song, int position) {
        Intent intent = new Intent(context, Music_Detail_Activity.class);
        intent.putExtra("zeroId", song.get(0).get_id());
        intent.putExtra("oneId", song.get(1).get_id());
        intent.putExtra("position", position);
        startActivity(intent);

    }


    @Override
    public void onSignerItemClick(SearchSinger singer) {
        Intent intent = new Intent(context, SearchSingerActivity.class);
        intent.putExtra("id", singer.get_id());
        startActivity(intent);
    }

    @Override
    protected SearchPresenter createPresenter() {
        return new SearchPresenter(this);
    }

    @Override
    public void showResult(List list) {
        customDialog.dismiss();

        List<SearchSong> songList = list;
        adapter.addData(songList);

    }

    @Override
    public void showOtherResult(List list) {
        List<SearchSinger> signerList = list;
        adapter.addOtherData(signerList);

    }
}
